#include "stm32_common.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#define  DWT_CR      *(__IO uint32_t *)0xE0001000
#define  DWT_CYCCNT  *(__IO uint32_t *)0xE0001004
#define  DEM_CR      *(__IO uint32_t *)0xE000EDFC
#define  DEM_CR_TRCENA          (1 << 24)
#define  DWT_CR_CYCCNTENA       (1 <<  0)

char PrintBuf[64] = {0U};
#define ADC1_DR_ADDRESS    ((uint32_t)0x4001204C)

/*****************************************
函数名称:Share_Bspgpio_Init
函数作用:初始化需要使用到的gpio
备注: PB0--LED1
      PB1--LED2
      PB3--LED3
******************************************/
void Share_Bspgpio_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	/* GPIOB Periph clock enable */
    	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	//led
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 ;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);

}


/*****************************************
函数名称:Share_BspLEDOff
函数作用:初始化需要使用到的gpio
备注: 传入LED123
******************************************/
void Share_BspLEDOff(Led_TypeDef Led)
{
    switch(Led)
    {
	case LED1:
		GPIO_SetBits(GPIOB,GPIO_Pin_0);
		break;
	case LED2:
		GPIO_SetBits(GPIOB,GPIO_Pin_1);
		break;
	case LED3:
		GPIO_SetBits(GPIOB,GPIO_Pin_3);
		break;
	default:
		break;
    }
}

/*****************************************
函数名称:Share_BspLEDOn
函数作用:初始化需要使用到的gpio
备注: 传入LED123
******************************************/
void Share_BspLEDOn(Led_TypeDef Led)
{
    switch(Led)
    {
		case LED1:
			GPIO_ResetBits(GPIOB,GPIO_Pin_0);
			break;
		case LED2:
			GPIO_ResetBits(GPIOB,GPIO_Pin_1);
			break;
		case LED3:
			GPIO_ResetBits(GPIOB,GPIO_Pin_3);
			break;
		default:
			break;
    }
}

/*****************************************
函数名称:Share_BspLEDToggle
函数作用:初始化需要使用到的gpio
备注: 传入LED123
******************************************/
void Share_BspLEDToggle(Led_TypeDef Led)
{
    switch(Led)
    {
		case LED1:
			GPIO_ToggleBits(GPIOB,GPIO_Pin_0);
			break;
		case LED2:
			GPIO_ToggleBits(GPIOB,GPIO_Pin_1);
			break;
		case LED3:
			GPIO_ToggleBits(GPIOB,GPIO_Pin_3);
			break;
		default:
			break;
    }
}

void Share_Bspdelay_ms(unsigned int times)
{
    volatile uint32_t i,j;
    for(i=0;i<times;i++)
    {
     for(j=0;j<9*1000;j++);
    }
}


/*****************************************
函数名称:Share_BspUsart_Init
函数作用:串口初始化
备注: 使用PC10脚此处只实现了发送功能
******************************************/
void Share_BspUsart_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	//USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_BaudRate = 230400;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART3, &USART_InitStructure);
	USART_Cmd(USART3, ENABLE);
	USART_ClearFlag(USART3,USART_FLAG_TC);
}

/*****************************************
函数名称:Share_BspSendDate
函数作用:串口发送功能
备注:
******************************************/
void Share_BspSendDate( uint8_t Data )
{
  USART_SendData(USART3, (uint8_t) Data);
  while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}

/*
 * 发送一个字符串
 */
void Share_BspSendStr(char* str)
{
  uint8_t count = 0;
  uint8_t len = 0;
  len = strlen(str);
  for(count=0;count<len;count++)
  {
    Share_BspSendDate(*str);
    str++;
  }
}

void Share_Printf(char *fmt_ptr, ...)
{
    va_list ap;
    //int ret;
    //int logLength = 0;
    va_start(ap, fmt_ptr);
    //解析数据
    vsprintf(PrintBuf,fmt_ptr,ap);
    Share_BspSendStr(PrintBuf);
    //发送
    va_end(ap);
}

/*****************************************
函数名称:Share_BspInit
函数作用:硬件初始化接口
备注:
******************************************/
void Share_BspInit(void)
{
	Share_Bspgpio_Init();
	Share_BspUsart_Init();
}







